package com.spzx.user.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.service.RegionService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.service.IUserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2024-07-10
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {
    @Resource
    private UserAddressMapper userAddressMapper;
    @Autowired
    RegionService regionService;



    @Override
    public List<UserAddress> getAddressList() {
        //获取userId
        Long userId = SecurityContextHolder.getUserId();
        //根据userId获取地址信息
        QueryWrapper<UserAddress> userAddressQueryWrapper = new QueryWrapper<>();
        userAddressQueryWrapper.eq("user_id", userId);
        List<UserAddress> userAddresses = userAddressMapper.selectList(userAddressQueryWrapper);
        return userAddresses;
    }

    //新增
    @Override
    public void addUserAddress(UserAddress userAddress) {
        //联系人，手机号，地址（省，市，县，区），详细地址，标签，设为默认
        //用户id，完整地址
        //获取userId
        Long userId = SecurityContextHolder.getUserId();
        userAddress.setUserId(userId);
        //根据code获取name
        String provinceCode = userAddress.getProvinceCode();
        String provinceName = regionService.getNameByCode(provinceCode);
        String cityCode = userAddress.getCityCode();
        String cityName = regionService.getNameByCode(cityCode);
        String districtCode = userAddress.getDistrictCode();
        String districtName = regionService.getNameByCode(districtCode);
        //获取完整地址
        String fullAddress = provinceName + cityName + districtName + userAddress.getAddress();
        userAddress.setFullAddress(fullAddress);
        userAddressMapper.insert(userAddress);
    }

    //修改
    @Override
    public void updateUserAddress(UserAddress userAddress) {
        Long userId = SecurityContextHolder.getUserId();
        UpdateWrapper<UserAddress> userAddressUpdateWrapper = new UpdateWrapper<>();
        //根据name获取code
        String provinceCode = regionService.getCodeByName(userAddress.getProvinceCode());
        String cityCode = regionService.getCodeByName(userAddress.getCityCode());
        String districtCode = regionService.getCodeByName(userAddress.getDistrictCode());
        userAddressUpdateWrapper.eq("user_id", userId)
                .set("name", userAddress.getName())
                .set("phone", userAddress.getPhone())
                .set("province", provinceCode)
                .set("city", cityCode)
                .set("distract", districtCode)
                .set("address", userAddress.getAddress())
                .set("full_address", userAddress.getName())
                .set("tag_name", userAddress.getTagName())
                .set("is_default", userAddress.getIsDefault());
        this.update(userAddressUpdateWrapper);
    }




}


